package com.ssy.lingxi.settle.accounts.serviceimpl;

import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.settle.accounts.entity.PlatformInvoiceDO;
import com.ssy.lingxi.settle.accounts.model.constant.CommonBooleanEnum;
import com.ssy.lingxi.settle.accounts.model.constant.InvoiceKindEnum;
import com.ssy.lingxi.settle.accounts.model.constant.InvoiceTypeEnum;
import com.ssy.lingxi.settle.accounts.model.vo.request.DeletePlatformInvoiceVO;
import com.ssy.lingxi.settle.accounts.model.vo.request.PlatformInvoiceAddVO;
import com.ssy.lingxi.settle.accounts.model.vo.request.PlatformInvoiceUpdateVO;
import com.ssy.lingxi.settle.accounts.model.vo.response.PlatformInvoiceDetailVO;
import com.ssy.lingxi.settle.accounts.model.vo.response.PlatformInvoiceVO;
import com.ssy.lingxi.settle.accounts.repository.PlatformInvoiceRepository;
import com.ssy.lingxi.settle.accounts.service.IPlatformInvoiceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 平台发票接口实现
 * @author 伍将
 * @date 2020/10/20
 * @version 2.0.0
 */
@Service
public class PlatformInvoiceServiceImpl implements IPlatformInvoiceService {

    @Resource
    private PlatformInvoiceRepository repository;

    /**
     * 获取平台发票列表
     * @author 伍将
     * @date 2020/10/20
     * @return com.ssy.lingxi.common.response.Wrapper<java.util.List < com.ssy.lingxi.settle.accounts.mode.vo.response.PlatformInvoiceVO>>
     **/
    @Override
    public Wrapper<List<PlatformInvoiceVO>> getList() {

        List<PlatformInvoiceVO> resultList = repository.findAll().stream()
                .sorted(Comparator.comparing(PlatformInvoiceDO::getIsDefault).reversed())
                .map(r -> {
                    PlatformInvoiceVO query = new PlatformInvoiceVO();
                    query.setId(r.getId());
                    query.setKindName(InvoiceKindEnum.getMessage(r.getKind()));
                    query.setTypeName(InvoiceTypeEnum.getMessage(r.getType()));
                    query.setInvoiceTitle(r.getInvoiceTitle());
                    query.setTaxNo(r.getTaxNo());
                    query.setBankOfDeposit(r.getBankOfDeposit());
                    query.setAccount(r.getAccount());
                    query.setAddress(r.getAddress());
                    query.setTel(r.getTel());
                    query.setDefaultName(CommonBooleanEnum.getMessage(r.getIsDefault()));

                    return query;
                }).collect(Collectors.toList());

        return Wrapper.success(resultList);
    }

    /**
     * 新增平台发票
     * @author 伍将
     * @date 2020/10/20
     * @param sysUser:
     * @param addVO:
     * @return com.ssy.lingxi.common.response.Wrapper
     **/
    @Transactional
    @Override
    public Wrapper<Long> add(UserLoginCacheDTO sysUser, PlatformInvoiceAddVO addVO) {

        PlatformInvoiceDO entity = new PlatformInvoiceDO();
        entity.setKind(addVO.getKind());
        entity.setType(addVO.getType());
        entity.setInvoiceTitle(addVO.getInvoiceTitle());
        entity.setTaxNo(addVO.getTaxNo());
        entity.setBankOfDeposit(addVO.getBankOfDeposit());
        entity.setAccount(addVO.getAccount());
        entity.setAddress(addVO.getAddress());
        entity.setTel(addVO.getTel());
        entity.setIsDefault(addVO.getIsDefault());
        entity.setCreateTime(System.currentTimeMillis());
        entity.setUpdateTime(0L);

        // 如果设置当前发票为默认时，更新其他发票为非默认
        if (addVO.getIsDefault().equals(CommonBooleanEnum.YES.getCode())) {
            repository.updateDefault(CommonBooleanEnum.NO.getCode(), System.currentTimeMillis());
        }

        repository.saveAndFlush(entity);

        return Wrapper.success(entity.getId());
    }

    /**
     * 更新平台发票
     * @author 伍将
     * @date 2020/10/20
     * @param sysUser:
     * @param updateVO:
     * @return com.ssy.lingxi.common.response.Wrapper
     **/
    @Override
    public Wrapper<Void> update(UserLoginCacheDTO sysUser, PlatformInvoiceUpdateVO updateVO) {

        PlatformInvoiceDO entity = repository.findById(updateVO.getId()).orElse(null);
        if (entity == null) {
            throw new BusinessException(ResponseCode.SA_PLATFORM_INVOICE_NOT_EXIST);
        }

        entity.setKind(updateVO.getKind());
        entity.setType(updateVO.getType());
        entity.setInvoiceTitle(updateVO.getInvoiceTitle());
        entity.setTaxNo(updateVO.getTaxNo());
        entity.setBankOfDeposit(updateVO.getBankOfDeposit());
        entity.setAccount(updateVO.getAccount());
        entity.setAddress(updateVO.getAddress());
        entity.setTel(updateVO.getTel());
        entity.setIsDefault(updateVO.getIsDefault());
        entity.setUpdateTime(System.currentTimeMillis());

        // 如果设置当前发票为默认时，更新其他发票为非默认
        if (updateVO.getIsDefault().equals(CommonBooleanEnum.YES.getCode())) {
            repository.updateDefault(CommonBooleanEnum.NO.getCode(), System.currentTimeMillis());
        }

        repository.saveAndFlush(entity);

        return Wrapper.success();
    }

    /**
     * 删除平台发票
     * @author 伍将
     * @date 2020/10/20
     * @param deleteVo:
     * @param sysUser:
     * @return com.ssy.lingxi.common.response.Wrapper
     **/
    @Override
    public Wrapper<Void> delete(UserLoginCacheDTO sysUser, DeletePlatformInvoiceVO deleteVo) {
        PlatformInvoiceDO entity = repository.findById(deleteVo.getDataId()).orElse(null);
        if (entity == null) {
            throw new BusinessException(ResponseCode.SA_PLATFORM_INVOICE_NOT_EXIST);
        }

        repository.deleteById(deleteVo.getDataId());

        return Wrapper.success();
    }

    /**
     * 获取平台发票详情
     * @author 伍将
     * @date 2020/10/20
     * @param id:
     * @return com.ssy.lingxi.common.response.Wrapper<com.ssy.lingxi.settle.accounts.mode.vo.response.PlatformInvoiceDetailVO>
     **/
    @Override
    public Wrapper<PlatformInvoiceDetailVO> getDetail(Long id) {
        PlatformInvoiceDO entity = repository.findById(id).orElse(null);
        if (entity == null) {
            throw new BusinessException(ResponseCode.SA_PLATFORM_INVOICE_NOT_EXIST);
        }

        PlatformInvoiceDetailVO result = new PlatformInvoiceDetailVO();
        result.setId(entity.getId());
        result.setKind(entity.getKind());
        result.setType(entity.getType());
        result.setInvoiceTitle(entity.getInvoiceTitle());
        result.setTaxNo(entity.getTaxNo());
        result.setBankOfDeposit(entity.getBankOfDeposit());
        result.setAccount(entity.getAccount());
        result.setAddress(entity.getAddress());
        result.setTel(entity.getTel());
        result.setIsDefault(entity.getIsDefault());

        return Wrapper.success(result);
    }

    /**
     * 获取默认发票
     * @author 伍将
     * @date 2021/1/21
     * @return 平台默认发票
     **/
    @Override
    public Wrapper<PlatformInvoiceDetailVO> getDefaultDetail() {
        List<PlatformInvoiceDO> resultList = repository.findAllByIsDefault(CommonBooleanEnum.YES.getCode());
        if (resultList.size() > 0) {
            PlatformInvoiceDetailVO result = new PlatformInvoiceDetailVO();
            result.setId(resultList.get(0).getId());
            result.setKind(resultList.get(0).getKind());
            result.setType(resultList.get(0).getType());
            result.setInvoiceTitle(resultList.get(0).getInvoiceTitle());
            result.setTaxNo(resultList.get(0).getTaxNo());
            result.setBankOfDeposit(resultList.get(0).getBankOfDeposit());
            result.setAccount(resultList.get(0).getAccount());
            result.setAddress(resultList.get(0).getAddress());
            result.setTel(resultList.get(0).getTel());
            result.setIsDefault(resultList.get(0).getIsDefault());
            return Wrapper.success(result);
        } else {
            return Wrapper.success(null);
        }
    }
}
